package com.pms.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.pms.entity.Contact;
import com.pms.entity.Project;
import com.pms.util.DBHelp;
import com.pms.util.PkUtil;
import com.pms.util.RowMapper;

public class ContactDao {
	
	DBHelp<Contact> db = new DBHelp<Contact>();
	
	public class ContactRowMapper implements RowMapper<Contact>{
		
		@Override
		public Contact mapRow(ResultSet rs) throws SQLException {
		
			Contact contact = new Contact();
			
			contact.setId(rs.getString("id"));
			contact.setName(rs.getString("name"));
			contact.setDescribe(rs.getString("describe"));
			contact.setCompany(rs.getString("company"));
			contact.setTelephone(rs.getString("telephone"));
			contact.setPhone(rs.getString("phone"));
			contact.setEmail(rs.getString("email"));
			contact.setAddress(rs.getString("address"));
			contact.setIndex(rs.getString("index"));
			contact.setBolgType(rs.getString("bolgtype"));
			contact.setUsername(rs.getString("username"));
			contact.setProjectId(rs.getString("t_project_id"));
			
			ProjectDao projectDao = new ProjectDao();
			
			Project project = projectDao.findById(rs.getString("t_project_id"));
			contact.setProject(project);
			
			return contact;
			}	
	}

	public List<Contact> findForList(String projectId) {
		String sql="select id ,`name`,`describe`,company,telephone,phone,email,address,`index`,bolgtype,username,t_project_id from t_contact where t_project_id = ?";
		return db.executeQueryForList(sql, new ContactRowMapper(),projectId);
	}

	public Contact findContactById(String contactId) {
		String sql = "select id ,`name`,`describe`,company,telephone,phone,email,address,`index`,bolgtype,username,t_project_id from t_contact where id= ?";
		return db.executeQueryForObject(sql, new ContactRowMapper(), contactId);
	}

	public void newContact(String name, String company, String telephone,
			String phone, String email, String address, String index,
			String bolgType,String username, String describe, String projectId) {
			String sql="INSERT INTO t_contact (id,`name`,`describe`,company,telephone,phone,email,address,`index`,bolgType,username,t_project_id)VALUES (?,?,?,?,?,?,?,?,?,?,?,?);";
			db.executeSQL(sql,PkUtil.getPk(),name,describe,company,telephone,phone,email,address,index,bolgType,username,projectId);
	}

	public void editContact(String name, String company, String telephone,
			String phone, String email, String address, String index,
			String bolgType, String username, String describe, String id) {
		
		String sql = "update t_contact set `name`=?, `describe`=?, company=?, telephone=?, phone=?,email =?, address=?, `index`=?, bolgtype=?, username=? where id=?";
		db.executeSQL(sql,name,describe,company,telephone,phone,email,address,index,bolgType,username,id);
	}

	public void deleteById(String contactId) {
		String sql ="delete from t_contact where id=?";
		db.executeSQL(sql, contactId);
	}
}
